//基于select选择的Zepto 福利多选 包装集的插件选择
(function($){
    $.fn.select_welfare = function(options){
        var opts = $.extend({
            select_title: '',              //select选项值（标题值）
            hidden:'',                     //隐藏表单id值(传入多个)
            opt_data: '',                  //select福利选项值（数组json对象）
            select_text:'.select_text',    //默认为当前select文本值
            select_con:"#select_con",      //追加的内容(快捷方式无啥用)
            welfareSelect:"#welfareSelect",//追加福利前缀(可忽略快捷方式无啥用)
            callback:function(welfareArr){}    //成功确定以后执行的回调函数
        },options||{}); //合并内容
        //直接指向this
        var othis = this;
        var welfareArr=[];
        var set_height;
        var handler;//设置事件禁止开启参数
        var firstScroll;
        var select_con_width;
        //获取福利
        if( $(opts.hidden).val() != ''){
            othis.find(opts.select_text).html(mSubstr($(opts.hidden).val(),32));
        }else{
            othis.find(opts.select_text).html('请选择职位诱惑');
        }
        //点击福利
        othis.click(function(){
            if( $(opts.hidden).val() != ''){
                welfareArr = othis.find(opts.hidden).val().split(",");
            }
            if ($(opts.select_con).length > 0) {
                return false;
            }
            //加载福利
            load_welfare();

            firstScroll=new IScroll(opts.select_con+" #first",{preventDefault:false });
            //设置事件禁止开启参数
            handler=function (e) { e.preventDefault();};
            document.addEventListener('touchmove', handler, isPassive() ? {
                capture: false,
                passive: false
            } : false);

            //加载选项
            loadSelect();
        });

        //加载选项
        function  loadSelect(){
            if(welfareArr.length > 0){
                //隐藏表单是不有值
                var str = '';
                var welfareSelect = str_trim(opts.welfareSelect);
                for(var i=0;i<welfareArr.length;i++){
                    $(opts.select_con).find('.select_list').find('li a').each(function(){
                        if($(this).find('span').html() == welfareArr[i]){
                            $(this).parent('li').addClass('sel');
                            str+='<span id="'+ welfareSelect + $(this).attr('data-id') +'">'+welfareArr[i]+'<i data-id="'+$(this).attr('data-id')+'"></i></span>';
                        }
                    });
                }
                $(opts.select_con).find('.selected').find('dd').append(str);
                setHeight();
                //点击关闭选项
                $(opts.select_con).find('.selected').find('span i').click(function(){
                    $(this).parent('span').remove();
                    var cthis =  $(this);
                    $(opts.select_con).find('.select_list').find('li a').each(function(){
                        if($(this).attr('data-id') == cthis.attr('data-id')){
                            $(this).parent('li').removeClass('sel');
                            //删除数组中的值
                            for(var i=0;i<welfareArr.length;i++){
                                if(welfareArr[i] == $(this).find('span').html()){
                                    welfareArr.splice(i,1);
                                }
                            }
                        }
                    });
                    setHeight();
                });

            }
            //关闭加载层 清除select
            $(opts.select_con).find('.close').click(function(){
                assignment();
                //设置事件禁止开启参数
                document.removeEventListener('touchmove',handler,false);
            });
            //确定按钮-------------------------------------------------------------------------------------------------
            $(opts.select_con).find('#determine').click(function(){
                if(welfareArr.length > 0){
                    othis.find(opts.select_text).html(mSubstr(welfareArr.join(","),32));
                    othis.find(opts.hidden).val(welfareArr.join(","));
                }else{
                    othis.find(opts.select_text).html('请选择职位诱惑');
                    othis.find(opts.hidden).val('');
                }
                assignment();
                //确定以后执行的回调函数
                opts.callback(welfareArr);
                //设置事件禁止开启参数
                document.removeEventListener('touchmove',handler,false);
            });
            //点击快选
            $(opts.select_con).find('.select_list').find('li a').each(function(index){
                $(this).click(function(){
                    setSelect($(this));
                    setHeight();
                });
            });
        }
        //设置监听滚动
        function isPassive() {
            var supportsPassiveOption = false;
            try {
                addEventListener("test", null, Object.defineProperty({}, 'passive', {
                    get: function () {
                        supportsPassiveOption = true;
                    }
                }));
            } catch(e) {}
            return supportsPassiveOption;
        }
        //添加已选内容
        function  setSelect(selthis,level){
            //获取当前的福利和cid
            var curr_welfare = selthis.find('span').html();
            var curr_cid = selthis.attr('data-id');
            if(selthis.parent('li').hasClass('sel')){
                selthis.parent('li').removeClass('sel');
                $(opts.select_con).find('.selected').find('dd').find(opts.welfareSelect+curr_cid).remove();
                //删除数组中的值
                for(var i=0;i<welfareArr.length;i++){
                    if(welfareArr[i] == curr_welfare){
                        welfareArr.splice(i,1);
                    }
                }
            }else{
                selthis.parent('li').addClass('sel');
                var str = '';
                var welfareSelect = str_trim(opts.welfareSelect);
                str+='<span id="'+welfareSelect + curr_cid+'">'+curr_welfare+'<i data-id="'+curr_cid+'"></i></span>';
                $(opts.select_con).find('.selected').find('dd').append(str);
                //添加到数组
                welfareArr.push(curr_welfare);
                //console.log(welfareArr);
            }
            //点击关闭按钮 删除选项
            $(opts.select_con).find('.selected').find(opts.welfareSelect+curr_cid).find('i').click(function(){
                var cthis = $(this);
                $(this).parent('span').remove();
                if(selthis.attr('data-id') == cthis.attr('data-id')){
                    selthis.parent('li').removeClass('sel');
                }
                //删除数组中的值
                for(var i=0;i<welfareArr.length;i++){
                    if(welfareArr[i] == curr_welfare){
                        welfareArr.splice(i,1);
                    }
                }
                setHeight();
            });
        }
        //设置高度
        function setHeight(){
            //有没有添加选项设置高度
            if($(opts.select_con).find('.selected').find('span').size() > 0){
                $(opts.select_con).find('.selected').show();
                var selectedHeight = $(opts.select_con).find('.selected').height();
                //设置最大高度
                set_height = $(opts.select_con).height() - $(opts.select_con).find('.select_header').height() - selectedHeight;
                $(opts.select_con).find('.select_list').find("#first").css({
                    'height':set_height + 'px'
                });
                firstScroll.refresh();
            }else{
                $(opts.select_con).find('.selected').hide();
                set_height = $(opts.select_con).height() - $(opts.select_con).find('.select_header').height();
                $(opts.select_con).find('.select_list').find("#first").css({
                    'height':set_height + 'px'
                });
                firstScroll.refresh();
            }

        }
        //加载内容
        function load_welfare(){
            var select_con = str_trim(opts.select_con);
            var str = '';
            if($('.masking').length <= 0){
                str+='<div class="masking" id="masking"></div>';
            }
            str+='<div class="select_con" id="'+select_con+'">';
            str+='<div class="select_header">';
            str+='<a class="close"></a>';
            str+='<div class="title">'+opts.select_title+'</div>';
            str+='<a id="determine" class="determine">确定</a>';
            str+='</div>';


            str+='<div class="select_list">';

            str+='<div class="selected">';
            str+='<dl>';
            str+='<dt>已选：</dt>';
            str+='<dd>';
            str+='</dd>';
            str+='</dl>';
            str+='</div>';


            str+='<div id="first">';
            str+='<ul>';
            //循环福利
            if(opts.opt_data.length > 0){
                for(var i=0;i<opts.opt_data.length;i++){
                    str+='<li><a data-id="'+ i +'"><span>'+ mSubstr(opts.opt_data[i],36) +'</span></a></li>';
                }
            }
            str+='</ul>';
            str+='</div>';
            str+='</div>';

            str+='</div>';
            //追击添加的内容
            $('body').append(str);

            $(".masking").show();
            //当文档的宽度大于内容的宽度时
            if($(document).width() > $(opts.select_con).width()){
                select_con_width = ( $(document).width() -  $(opts.select_con).width() )/2 ;
                $(opts.select_con).animate({
                    left:select_con_width + 'px',
                    'animation':'select_start 0.4s 0s ease',
                    'display':'block'
                }, 300);
            }else{
                $(opts.select_con).animate({
                    left:'0',
                    'animation':'select_start 0.4s 0s ease',
                    'display':'block'
                }, 300);
            }

        }
        //赋值内容
        function assignment(){
            $(opts.select_con).animate({
                'left':'7.5rem',
                'animation':'select_end .4s 0s ease',
                'display':'block'
            }, 300 ,function(){
                $(opts.select_con).remove();
                $(".masking").hide();
            });
        }
        return this;
    };
})(Zepto);

